package com.yanqu.road.server.cmd.activity.summerquiz;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.activity.summerquiz.SummerQuizActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

@Cmd(code = Protocol.S_SUMMER_QUIZ_USER_GUESS_GET_REWARD, desc = "竞猜领取奖励")
@CmdApi(reqMsg = SummerQuizProto.SummerQuizGuessGetRewardReqMsg.class, respMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.class)
public class SummerQuizGuessGetRewardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerQuizConfig config = SummerQuizActivityMgr.getConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerQuizProto.SummerQuizGuessGetRewardReqMsg reqMsg = SummerQuizProto.SummerQuizGuessGetRewardReqMsg.parseFrom(packet.getBytes());

        SummerQuizInfo summerQuizInfo = config.getSummerQuizInfo(reqMsg.getQuizId());
        if(summerQuizInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if(summerQuizInfo.getResult() == 0){
            ret = GameErrorCode.E_SUMMER_QUIZ_NOT_IN_REWARD_TIME;
            return;
        }

        //时间判断
        if(System.currentTimeMillis() <= summerQuizInfo.getDate() + DateHelper.DAY_MILLIONS){
            ret = GameErrorCode.E_SUMMER_QUIZ_NOT_IN_REWARD_TIME;
            return;
        }

        if(!SystemOpenMgr.systemOpen(player, eSystemId.SummerQuizActivity.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        //看竞猜积分道具
        SummerGamesConfig gamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (gamesConfig == null || !ActivityMgr.activityInShowTime(gamesConfig.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
//        int guessScoreGoodsId = gamesConfig.getEXCHANGE_SCORE_ITEM_ID_2();
//        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(guessScoreGoodsId);
//        if(goodsInfo == null){
//            ret = GameErrorCode.E_GAME_ERROR;
//            return;
//        }

        SummerQuizProto.CrossSummerQuizGuessGetRewardReqMsg.Builder crossReq = SummerQuizProto.CrossSummerQuizGuessGetRewardReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityInfo().getActivityId())
                .setQuizId(summerQuizInfo.getId())
                .setGameActivityId(gamesConfig.getActivityId())
                .setScoreGoodsId(GameConfig.GAME_MONEY_INGOTS);
        player.sendPacket(Protocol.C_SUMMER_QUIZ_GUESS_GET_USER_REWARD, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder().setRet(ret));
        }
    }
}
